package class_object;

public class InitializationDemo {
    // 1. 默认初始化（由JVM完成）
    private int defaultInit;

    // 2. 显式初始化
    private int explicitInit = 10;

    // 3. 静态属性显式初始化
    private static int staticExplicitInit = 100;

    // 4. 实例初始化块
    {
        explicitInit = 20;
        System.out.println("实例初始化块执行");
    }

    // 5. 静态初始化块
    static {
        staticExplicitInit = 200;
        System.out.println("静态初始化块执行");
    }

    // 6. 构造方法初始化
    public InitializationDemo() {
        explicitInit = 30;
        System.out.println("无参构造方法执行");
    }

    public InitializationDemo(int value) {
        explicitInit = value;
        System.out.println("有参构造方法执行");
    }

    public void display() {
        System.out.println("defaultInit: " + defaultInit); // 默认值0
        System.out.println("explicitInit: " + explicitInit);
        System.out.println("staticExplicitInit: " + staticExplicitInit);
    }

    public static void main(String[] args) {
        System.out.println("创建第一个对象：");
        InitializationDemo obj1 = new InitializationDemo();
        obj1.display();

        System.out.println("\n创建第二个对象：");
        InitializationDemo obj2 = new InitializationDemo(40);
        obj2.display();
    }
}
